<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        button {
            position: fixed;
            right: 50px;
            bottom: 50px;
        }
    </style>
</head>

<body style="height:3000px">
    <div id="app">
        <input type="text" v-focus="123">
        <input type="text" v-focus2>
        <button v-top="500">回到顶部</button>
    </div>
    <script src="./vue.js"></script>
    <script>
        // 定义一个指令
        Vue.directive("focus", {
            bind(el, binding) {
                console.log("初始化调用");
                console.log(el);
                console.log(binding)
            },
            inserted(el, binding) {
                console.log("绑定的元素插入到dom中啦")
                el.focus();
            }
        })
        Vue.directive("top", {
            inserted(el, binding) {
                console.log(binding)
                el.style.display = "none";
                window.onscroll = function () {
                    // 获取滚动距离
                    var sTop = document.documentElement.scrollTop || document.body.scrollTop;
                    if (sTop > binding.value) {
                        el.style.display = "block";
                    } else {
                        el.style.display = "none";
                    }
                }
            },
            unbind() {
                window.onscroll = null;
            }
        })
        let vm = new Vue({
            el: "#app",
            directives: {
                focus2: {
                    inserted(el, binding) {
                        el.focus();
                    }
                }
            }
        })
    </script>
</body>

</html>